package com.woniu.userassembly.identifycontroller.impl;




import com.woniu.userassembly.identifycontroller.util.Base64Util;
import com.woniu.userassembly.identifycontroller.util.FileUtil;
import com.woniu.userassembly.identifycontroller.util.HttpUtil;

import java.net.URLEncoder;

public class HouseholdRegister {
    public static String householdRegister(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/household_register";
        try {
            // 本地文件路径
            //String filePath = "[本地文件路径]";
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AuthService.getaccessToken();

            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
